package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.mq.constant.MqConst;
import com.atguigu.ssyx.mq.service.RabbitService;
import com.atguigu.ssyx.product.service.SkuInfoService;
import com.atguigu.ssyx.vo.product.SkuInfoQueryVo;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * sku信息 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-09-13
 */
@Api("SKU信息管理接口")
@RestController
@RequestMapping("/admin/product/skuInfo")

public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private RabbitService rabbitService;

    //SKU列表功能
//    url: `${api_name}/${page}/${limit}`,
//    method: 'get',
//    params: searchObj
    @ApiOperation("SKU列表接口")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       SkuInfoQueryVo skuInfoQueryVo) {
        Page<SkuInfo> skuInfoPrgram=new Page(page, limit);
       IPage<SkuInfo> pagemodel =skuInfoService.selectSkuInfoPage(skuInfoPrgram,skuInfoQueryVo);
        return Result.ok(pagemodel);
    }

//    url: `${api_name}/save`,
//    method: 'post',
//    data: role
@ApiOperation("添加商品sku信息")
@PostMapping("/save")
public Result save(@RequestBody SkuInfoVo skuInfoVo) {
        skuInfoService.saveSkuInfo(skuInfoVo);
        return Result.ok(null);
}
//根据id获取sku的信息
//    url: `${api_name}/get/${id}`,
//    method: 'get'
    @ApiOperation("根据id获取sku的信息")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SkuInfoVo skuInfoVo = skuInfoService.getSkuInfo(id);
        return Result.ok(skuInfoVo);
    }
    //修改sku信息
//    url: `${api_name}/update`,
//    method: 'put',
//    data: role
   @ApiOperation("修改sku信息")
   @PutMapping("/update")
    public Result update(@RequestBody SkuInfoVo skuInfoVo) {
        skuInfoService.updateSkuInfo(skuInfoVo);
        return Result.ok(null);
   }


    @ApiOperation(value = "删除商品sku信息")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        skuInfoService.removeById(id);
        //删除商品同时同步到es
        rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT,
                MqConst.ROUTING_GOODS_LOWER,
                id);

        return Result.ok(null);
    }

    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        skuInfoService.removeByIds(idList);
        for (Long id : idList) {
            //删除商品同时同步到es
            rabbitService.sendMessage(MqConst.EXCHANGE_GOODS_DIRECT,
                    MqConst.ROUTING_GOODS_LOWER,
                    id);
        }
        return Result.ok(null);
    }

    @ApiOperation("商品审核")
    @GetMapping("/check/{id}/{status}")
    public Result check(@PathVariable("id") Long Skuid, @PathVariable("status") Integer status) {
        skuInfoService.check(Skuid,status);
        return Result.ok(null);
    }

    @ApiOperation("商品上下架")
    @GetMapping("/publish/{id}/{status}")
    public Result publish(@PathVariable("id") Long Skuid, @PathVariable("status") Integer status) {
        skuInfoService.publish(Skuid,status);
        return Result.ok(null);
    }

    @ApiOperation("新人专享")
    @GetMapping("/isNewPerson/{id}/{status}")
    public Result isNewPerson(@PathVariable("id") Long Skuid, @PathVariable("status") Integer status) {
        skuInfoService.isNewPerson(Skuid,status);
        return Result.ok(null);
    }

}

